package no.ntnu.fp.mmi;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;

import no.ntnu.fp.su.Log;
import no.ntnu.fp.su.LogEntry;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class ShowLogPanel extends javax.swing.JFrame {
	private JLabel intervallButton;
	private JLabel yearText;
	private JComboBox fromYearBox;
	private JComboBox toYearBox;
	private JComboBox fromDayBox;
	private JComboBox toDayBox;
	private JComboBox fromMonthBox;
	private JComboBox toMonthBox;
	private JLabel dayText;
	// private ComboBoxModel startTimeModel;
	private JComboBox fromSecBox;
	private JComboBox fromMinuteBox;
	private JComboBox fromHourBox;
	private JComboBox toSecBox;
	private JComboBox toMinuteBox;
	private JComboBox toHourBox;
	private JLabel mounthText;
	private JLabel secText;
	private JLabel minuteText;
	private JLabel hourText;
	private JButton showButton;
	private JSeparator jSeparator1;
	private JButton closeButton;
	private JRadioButton todayButton;
	private JLabel toText;
	private ButtonGroup group;
	private LogModel log;
	private Timestamp time;
	private Font font = new Font("Candera", Font.BOLD, 16);
	private JLabel intText;
	private JRadioButton setDateButton;
	private ArrayList<LogEntry> logs;
	private String name;
	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Auto-generated main method to display this JFrame
	 */

	public ShowLogPanel(LogModel log, String name) {
		super();
		this.log = log;
		this.name = name;
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			setSize(500, 400);
			{
				intervallButton = new JLabel();
				getContentPane().add(intervallButton);
				intervallButton.setText("From");
				intervallButton.setBounds(27, 93, 143, 13);
			}
			{
				ImageIcon wellImage = new ImageIcon(getClass().getResource(
						"log.jpg"));
				JLabel inside1 = new JLabel();
				getContentPane().add(inside1);
				inside1.setIcon(wellImage);
				inside1.setBounds(27, 19, 35, 53);
				setVisible(true);

			}
			{
				dayText = new JLabel();
				getContentPane().add(dayText);
				dayText.setText("Day");
				dayText.setBounds(69, 110, 23, 18);
			}
			{
				ComboBoxModel mounthBoxModel = new DefaultComboBoxModel();
				fromMonthBox = new JComboBox(mounthBoxModel);
				getContentPane().add(fromMonthBox);
				fromMonthBox.setModel(mounthBoxModel);
				for (int i = 1; i < 13; i++) {
					if (i < 10)
						fromMonthBox.addItem("0" + i);
					else
						fromMonthBox.addItem(i);
				}

				fromMonthBox.setBounds(116, 128, 47, 24);
			}
			{
				ComboBoxModel toMounthBoxModel = new DefaultComboBoxModel();
				toMonthBox = new JComboBox(toMounthBoxModel);
				getContentPane().add(toMonthBox);
				for (int i = 1; i < 13; i++) {
					if (i < 10)
						toMonthBox.addItem("0" + i);
					else
						toMonthBox.addItem(i);
				}
				toMonthBox.setModel(toMounthBoxModel);
				toMonthBox.setBounds(116, 247, 53, 24);
			}
			{
				ComboBoxModel dayBoxModel = new DefaultComboBoxModel();
				fromDayBox = new JComboBox(dayBoxModel);
				getContentPane().add(fromDayBox);
				fromDayBox.setModel(dayBoxModel);
				fromDayBox.setBounds(63, 128, 47, 24);
				for (int i = 1; i < 32; i++) {
					if (i < 10)
						fromDayBox.addItem("0" + i);
					else
						fromDayBox.addItem(i);

				}
			}
			{
				ComboBoxModel todayBoxModel = new DefaultComboBoxModel();
				toDayBox = new JComboBox(todayBoxModel);
				getContentPane().add(toDayBox);
				toDayBox.setModel(todayBoxModel);
				toDayBox.setBounds(63, 247, 47, 24);
				for (int i = 1; i < 32; i++) {
					if (i < 10)
						toDayBox.addItem("0" + i);
					else
						toDayBox.addItem(i);
				}
			}
			{
				ComboBoxModel yearBoxModel = new DefaultComboBoxModel();
				fromYearBox = new JComboBox(yearBoxModel);
				getContentPane().add(fromYearBox);
				fromYearBox.setModel(yearBoxModel);
				fromYearBox.setBounds(175, 128, 73, 24);
				for (int i = 2010; i < 2051; i++) {

					fromYearBox.addItem(i);
				}
			}
			{
				ComboBoxModel toYearBoxModel = new DefaultComboBoxModel();
				toYearBox = new JComboBox(toYearBoxModel);
				getContentPane().add(toYearBox);
				toYearBox.setModel(toYearBoxModel);
				toYearBox.setBounds(175, 247, 73, 24);
				for (int i = 2010; i < 2051; i++) {
					toYearBox.addItem(i);
				}
			}
			{
				yearText = new JLabel();
				getContentPane().add(yearText);
				yearText.setText("Year");
				yearText.setBounds(175, 111, 79, 16);
			}
			{
				showButton = new JButton();
				getContentPane().add(showButton);
				showButton.setText("Show");
				showButton.setBounds(182, 288, 161, 28);
				showButton.addActionListener(new showButtonEvent());
			}
			{
				hourText = new JLabel();
				getContentPane().add(hourText);
				hourText.setText("Hour");
				hourText.setBounds(284, 106, 77, 26);
			}
			{
				minuteText = new JLabel();
				getContentPane().add(minuteText);
				minuteText.setText("Minute");
				minuteText.setBounds(341, 110, 79, 19);
			}
			{
				secText = new JLabel();
				getContentPane().add(secText);
				secText.setText("Seconds");
				secText.setBounds(402, 104, 79, 26);
			}
			{
				ComboBoxModel fromHourBoxModel = new DefaultComboBoxModel();
				fromHourBox = new JComboBox();
				getContentPane().add(fromHourBox);
				fromHourBox.setModel(fromHourBoxModel);
				fromHourBox.setBounds(284, 128, 47, 24);
				for (int j = 0; j < 24; j++) {
					if (j < 10)
						fromHourBox.addItem("0" + j);
					else
						fromHourBox.addItem(j);
				}

			}
			{
				ComboBoxModel toHourBoxModel = new DefaultComboBoxModel();
				toHourBox = new JComboBox();
				getContentPane().add(toHourBox);
				toHourBox.setModel(toHourBoxModel);
				toHourBox.setBounds(284, 246, 47, 24);
				for (int j = 0; j < 24; j++) {
					if (j < 10)
						toHourBox.addItem("0" + j);
					else
						toHourBox.addItem(j);
				}

			}
			{
				ComboBoxModel fromMinuteBoxModel = new DefaultComboBoxModel();
				fromMinuteBox = new JComboBox();
				getContentPane().add(fromMinuteBox);
				fromMinuteBox.setModel(fromMinuteBoxModel);
				fromMinuteBox.setBounds(343, 128, 47, 24);
				for (int i = 0; i < 60; i++) {
					if (i < 10)
						fromMinuteBox.addItem("0" + i);
					else
						fromMinuteBox.addItem(i);
				}
			}
			{
				ComboBoxModel toMinuteBoxModel = new DefaultComboBoxModel();
				toMinuteBox = new JComboBox();
				getContentPane().add(toMinuteBox);
				toMinuteBox.setModel(toMinuteBoxModel);
				toMinuteBox.setBounds(343, 246, 47, 24);
				for (int i = 0; i < 60; i++) {
					if (i < 10)
						toMinuteBox.addItem("0" + i);
					else
						toMinuteBox.addItem(i);
				}
			}
			{
				ComboBoxModel fromSecBoxModel = new DefaultComboBoxModel();
				fromSecBox = new JComboBox();
				getContentPane().add(fromSecBox);
				fromSecBox.setModel(fromSecBoxModel);
				fromSecBox.setBounds(402, 128, 47, 24);
				for (int i = 0; i < 60; i++) {
					if (i < 10)
						fromSecBox.addItem("0" + i);
					else
						fromSecBox.addItem(i);

				}
			}
			{
				ComboBoxModel toSecBoxModel = new DefaultComboBoxModel();
				toSecBox = new JComboBox();
				getContentPane().add(toSecBox);
				toSecBox.setModel(toSecBoxModel);
				toSecBox.setBounds(402, 246, 47, 24);
				for (int i = 0; i < 60; i++) {
					if (i < 10)
						toSecBox.addItem("0" + i);
					else
						toSecBox.addItem(i);

				}
			}
			{
				mounthText = new JLabel();
				getContentPane().add(mounthText);
				mounthText.setText("Month");
				mounthText.setBounds(116, 111, 33, 16);
			}
			{
				jSeparator1 = new JSeparator();
				getContentPane().add(jSeparator1);
				jSeparator1.setBounds(64, 169, 391, 11);
			}
			{
				toText = new JLabel();
				getContentPane().add(toText);
				toText.setText("To");
				toText.setBounds(29, 193, 14, 16);
			}
			{
				group = new ButtonGroup();

				todayButton = new JRadioButton();
				getContentPane().add(todayButton);
				todayButton.setText("Now");
				todayButton.setBounds(64, 199, 57, 18);
				todayButton.setSelected(true);
				toDayBox.setEnabled(false);
				toMonthBox.setEnabled(false);
				toYearBox.setEnabled(false);
				toHourBox.setEnabled(false);
				toMinuteBox.setEnabled(false);
				toSecBox.setEnabled(false);
				time = new Timestamp(System.currentTimeMillis());
				todayButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						toDayBox.setEnabled(false);
						toMonthBox.setEnabled(false);
						toYearBox.setEnabled(false);
						toHourBox.setEnabled(false);
						toMinuteBox.setEnabled(false);
						toSecBox.setEnabled(false);
						time = new Timestamp(System.currentTimeMillis());

					}
				});
				group.add(todayButton);
			}
			{
				setDateButton = new JRadioButton();
				getContentPane().add(setDateButton);
				setDateButton.setText("Set Date");
				setDateButton.setBounds(64, 223, 114, 18);
				setDateButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						toDayBox.setEnabled(true);
						toMonthBox.setEnabled(true);
						toYearBox.setEnabled(true);
						toHourBox.setEnabled(true);
						toMinuteBox.setEnabled(true);
						toSecBox.setEnabled(true);
					}
				});

				group.add(setDateButton);
			}
			{
				closeButton = new JButton();
				getContentPane().add(closeButton);
				closeButton.setText("Close");
				closeButton.setBounds(236, 322, 61, 28);
				closeButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();

					}
				});
			}
			{
				intText = new JLabel();
				getContentPane().add(intText);
				intText.setText("Choose intervals for " + name);
				intText.setBounds(62, 40, 347, 24);
				intText.setFont(font);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class showButtonEvent implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String fromDay = fromDayBox.getSelectedItem().toString();
			String fromMonth = fromMonthBox.getSelectedItem().toString();
			String fromYear = fromYearBox.getSelectedItem().toString();
			String fromHour = fromHourBox.getSelectedItem().toString();
			String fromMinute = fromMinuteBox.getSelectedItem().toString();
			String fromSec = fromSecBox.getSelectedItem().toString();
			String From = fromYear + "-" + fromMonth + "-" + fromDay + " "
					+ fromHour + ":" + fromMinute + ":" + fromSec;

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

			java.util.Date myFromDate = null;
			Timestamp fromTime = null;
			try {
				myFromDate = sdf.parse(From);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			fromTime = new Timestamp(myFromDate.getTime());

			if (toDayBox.isEnabled()) {
				String toDay = (String) toDayBox.getSelectedItem().toString();
				String toMonth = (String) toMonthBox.getSelectedItem()
						.toString();
				String toYear = (String) toYearBox.getSelectedItem().toString();
				String toHour = toHourBox.getSelectedItem().toString();
				String toMinute = toMinuteBox.getSelectedItem().toString();
				String toSec = toSecBox.getSelectedItem().toString();

				String to = toYear + "-" + toMonth + "-" + toDay + " " + toHour
						+ ":" + toMinute + ":" + toSec;

				java.util.Date myToDate = null;
				// Timestamp toTime = null;
				try {
					myToDate = sdf.parse(to);
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				time = new Timestamp(myToDate.getTime());
			}
			Log loggy = log.getIntLogList(fromTime, time);
			loggy.readComments();
			logs = loggy.getLog();

			if (logs.size() == 0) {
				JOptionPane.showMessageDialog(null,
						"There are no logs in this time interval", "No Logs",
						JOptionPane.INFORMATION_MESSAGE);
			} else {

				LogPanel gui = new LogPanel(logs, name, loggy);
				gui.setVisible(true);
			}
		}

	}

}
